package com.hanlink.dsp.ca.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.edge.pbx.view.PbxCircleSeekBar;
import com.edge.pbx.view.common.ChNameRadioView;
import com.edge.pbx.view.summing.PbxOutputSelectChannelView;
import com.edge.pbx.view.summing.PbxOutputSelectChannelsLayout;
import com.edge.pbx.view.xover.XoverSlopeWheelView;
import com.edge.pbxdsp.R;
import com.hanlink.dsp.DspSettings;

import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.ca.ui.base.BaseAppFragment;
import com.hanlink.dsp.ui.dialog.GroupingDialog;
import com.hanlink.dsp.ui.widget.KeyboardChangeListener;
import com.hanlink.dsp.ui.widget.XOverPassView;
import com.hanlink.dsp.ui.widget.XoverEditText;
import com.hanlink.dsp.ui.widget.chart.EqView;
import com.hanlink.dsp.util.DataUtil;
import com.hanlink.dsp.util.DrawFunction;
import com.hanlink.dsp.util.FreqUtil;
import com.hanlink.dsp.util.ViewUtil;
import com.qztech.sdk.model.Grouping;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import butterknife.BindView;
//import butterknife.BindViews;
//import butterknife.OnClick;
//import butterknife.OnLongClick;

import static com.hanlink.dsp.DspSettings.XOVER_SETTINGS;
import static com.hanlink.dsp.DspSettings.isSupportGroupAll;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_BAND_BPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_BAND_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_HIGH_BYPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_HIGH_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_LOW_BYPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_LOW_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_UNKNOWN;

@SuppressWarnings("unused")
public class XoverFragment extends BaseAppFragment implements XOverPassView.OnSlowCheckedChangeListener, GroupingDialog.OnGroupingChangedListener {


    //high
    //@BindView(R.id.xpv_high_pass)
    XOverPassView mXpvHighPass;
    //low
    //@BindView(R.id.xpv_low_pass)
    XOverPassView mXpvLowPass;


    private ImageButton pbx_xover_high_reset,pbx_xover_low_reset;


    boolean updateOk = false;
    private KeyboardChangeListener mKeyboardChangeListener;
    private GroupingDialog mGroupingDialog;


    //==================
    //@BindView(R.id.pbx_xover_whv_high)
    XoverSlopeWheelView mXoverSlopeHigh;
    //@BindView(R.id.pbx_xover_whv_low)
    XoverSlopeWheelView mXoverSlopeLow;
    //@BindView(R.id.esv_eqchart)
    EqView mEqChart;
    //@BindView(R.id.pbx_xover_highpass)
    ImageButton mRbHighPass;

    //@BindView(R.id.pbx_xover_high_bypass)
    ImageButton mRbHighByPass;

    //@BindView(R.id.pbx_xover_low_bypass)
    ImageButton mRbLowByPass;

    //@BindView(R.id.pbx_xover_lowpass)
    ImageButton mRbLowPass;


    //@BindViews({R.id.pbx_xover_highpass,R.id.pbx_xover_lowpass,R.id.pbx_xover_low_bypass, R.id.pbx_xover_high_bypass})
    ImageButton[] mPassButtons;
    private ChNameRadioView xover_link1,xover_link2,xover_link3,xover_link4;
    //@BindViews({R.id.xover_link1, R.id.xover_link2, R.id.xover_link3, R.id.xover_link4})
    ChNameRadioView[] mCbGroup;

    //@BindView(R.id.pbx_xover_seekbar_high)
    PbxCircleSeekBar mCircleSeekBarHigh;
    //@BindView(R.id.pbx_xover_seekbar_low)
    PbxCircleSeekBar mCircleSeekBarLow;

    //@BindView(R.id.pbx_xover_hight_et)
    XoverEditText mEditTextHigh;
    //@BindView(R.id.pbx_xover_low_et)
    XoverEditText mEditTextLow;

    //@BindView(R.id.pbx_output_selects_layout)
    PbxOutputSelectChannelsLayout mPbxOutputSelectChannelsLayout;
    private List<PbxOutputSelectChannelView> mOutputViews = new ArrayList<>();
    @Override
    protected int getResourceId() {
        return R.layout.fragment_xover;
//        return R.layout.fragment_xover_old;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();

        mXpvLowPass.setHzText(com.hanlink.res.R.string.hz);
        mKeyboardChangeListener = new KeyboardChangeListener(getActivity());
        mKeyboardChangeListener.setKeyBoardListener((isShow, keyboardHeight) -> {
            if (!isShow) {
                if (mEditTextHigh.isEnabled()){
                    mEditTextHigh.validEditTextValue();
                }
                else if (mEditTextLow.isEnabled()){
                    mEditTextLow.validEditTextValue();
                }
                if (mXpvLowPass.isEnable()) {
                    mXpvLowPass.validEditTextValue();
                } else if (mXpvHighPass.isEnable()) {
                    mXpvHighPass.validEditTextValue();
                }
            }
        });
        ViewUtil.findOutputViews(mOutputViews, view, mChannelSelectListener);

        mCircleSeekBarHigh.setOnCircleSeekBarListener(mCircleSeekBarListener);
        mCircleSeekBarLow.setOnCircleSeekBarListener(mCircleSeekBarListener);


        mCircleSeekBarHigh.setOnCircleSeekBarFunListener(mOnCircleSeekBarFunListener);
        mCircleSeekBarLow.setOnCircleSeekBarFunListener(mOnCircleSeekBarFunListener);

        mXoverSlopeHigh.setSelectItemListener(mSelectItemListener);
        mXoverSlopeLow.setSelectItemListener(mSelectItemListener);

        mEditTextHigh.setValidEditTextListener(mEditTextListener);
        mEditTextLow.setValidEditTextListener(mEditTextListener);
        mGroupingDialog = new GroupingDialog(getActivity());
        mGroupingDialog.setGroupingChangedListener(this);
    }

    private void initViews(){
        pbx_xover_low_reset = findViewById(R.id.pbx_xover_low_reset);
        pbx_xover_high_reset = findViewById(R.id.pbx_xover_high_reset);
        mXpvHighPass = findViewById(R.id.xpv_high_pass);
        mXpvLowPass = findViewById(R.id.xpv_low_pass);
        mXoverSlopeHigh = findViewById(R.id.pbx_xover_whv_high);
        mXoverSlopeLow = findViewById(R.id.pbx_xover_whv_low);
        mEqChart = findViewById(R.id.esv_eqchart);
        mRbHighPass = findViewById(R.id.pbx_xover_highpass);
        mRbHighByPass = findViewById(R.id.pbx_xover_high_bypass);
        mRbLowByPass = findViewById(R.id.pbx_xover_low_bypass);
        mRbLowPass = findViewById(R.id.pbx_xover_lowpass);

        mPassButtons = new ImageButton[]{mRbHighPass,mRbLowPass,mRbLowByPass,mRbHighByPass};

        for(ImageButton ib : mPassButtons){
            ib.setOnClickListener(onPassClick);
        }

        xover_link1 = findViewById(R.id.xover_link1);
        xover_link2 = findViewById(R.id.xover_link2);
        xover_link3 = findViewById(R.id.xover_link3);
        xover_link4 = findViewById(R.id.xover_link4);
        mCbGroup = new ChNameRadioView[]{xover_link1,xover_link2,xover_link3,xover_link4};

        for(ChNameRadioView cb : mCbGroup){
            cb.setOnClickListener(onGroupClick);
            cb.setOnLongClickListener(onGroupLongClick);
        }


        mCircleSeekBarHigh = findViewById(R.id.pbx_xover_seekbar_high);
        mCircleSeekBarLow = findViewById(R.id.pbx_xover_seekbar_low);
        mEditTextHigh = findViewById(R.id.pbx_xover_hight_et);
        mEditTextLow = findViewById(R.id.pbx_xover_low_et);
        mPbxOutputSelectChannelsLayout = findViewById(R.id.pbx_output_selects_layout);

        pbx_xover_low_reset.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                XOVER_SETTINGS.getCurrentChannel().resetHighPass();
                XOVER_SETTINGS.getCurrentChannel().sendCmd();
                updateData();
                return true;
            }
        });

        pbx_xover_low_reset.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                XOVER_SETTINGS.getCurrentChannel().resetLowPass();
                XOVER_SETTINGS.getCurrentChannel().sendCmd();
                updateData();
                return true;
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        ViewUtil.initCarType(mPbxOutputSelectChannelsLayout);
        updateData();
    }

    @Override
    public void onPause() {
        super.onPause();
        YzDebug.d("onPause");
        DataUtil.saveXoverSettingsToDb();
        updateOk = false;
    }

    @Override
    public void onStop() {
        if (mGroupingDialog != null && mGroupingDialog.isShowing()) {
            mGroupingDialog.dismiss();
        }
        mXpvHighPass.removeCallbacks(mSetListenerTask);
        super.onStop();
    }

    private void updateData() {
        try {
            updateOk = false;
            ViewUtil.initChannelName(mOutputViews, mCbGroup);
            ViewUtil.initOutputChannels(DspSettings.SUMMING_SETTINGS,mOutputViews);
            setGroupState();
            resetSelectedChannelStatus();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            updateOk = true;
        }
    }

    private void updateChValue() {
        updateOk = false;
        YzDebug.d("updateChValue");
        mXpvHighPass.setOnSlowCheckedChangeListener(null);
        mXpvLowPass.setOnSlowCheckedChangeListener(null);
        XoverChannel currentChannel = XOVER_SETTINGS.getXoverChannels().get(XOVER_SETTINGS.getLastChannel());
        if (currentChannel != null) {
            int passType = currentChannel.getPassType();
            YzDebug.d("updateChValue---passType:"+passType);
            switch (passType) {
                case PASS_TYPE_UNKNOWN:
                    setHighUiEnabled(false);
                    setLowUiEnabled(false);
                    mRbHighPass.setSelected(false);
                    mRbLowPass.setSelected(false);
                    mRbHighByPass.setSelected(false);
                    mRbLowByPass.setSelected(false);
                    break;
                case PASS_TYPE_HIGH_PASS:
                    setHighUiEnabled(true);
                    setLowUiEnabled(true);
                    mRbHighPass.setSelected(true);
                    mRbLowPass.setSelected(false);
                    mRbHighByPass.setSelected(false);
                    mRbLowByPass.setSelected(false);
                    break;
                case PASS_TYPE_LOW_PASS:
                    setHighUiEnabled(true);
                    setLowUiEnabled(true);
                    mRbHighPass.setSelected(false);
                    mRbLowPass.setSelected(true);
                    mRbHighByPass.setSelected(false);
                    mRbLowByPass.setSelected(false);
                    break;
                case PASS_TYPE_HIGH_BYPASS:

                    setHighUiEnabled(false);
                    setLowUiEnabled(false);
                    mRbHighPass.setSelected(false);
                    mRbLowPass.setSelected(false);
                    mRbHighByPass.setSelected(true);
                    mRbLowByPass.setSelected(false);
                    break;
                case PASS_TYPE_LOW_BYPASS:

                    setHighUiEnabled(false);
                    setLowUiEnabled(false);
                    mRbHighPass.setSelected(false);
                    mRbLowPass.setSelected(false);
                    mRbHighByPass.setSelected(false);
                    mRbLowByPass.setSelected(true);
                    break;
            }
            mCircleSeekBarHigh.setProgress(FreqUtil.parseFreqToProgress(currentChannel.getHighFrequency()));
            mCircleSeekBarLow.setProgress(FreqUtil.parseFreqToProgress(currentChannel.getLowFrequency()));
            mEditTextHigh.setText(String.valueOf(currentChannel.getHighFrequency()));
            mEditTextLow.setText(String.valueOf(currentChannel.getLowFrequency()));

            mXoverSlopeHigh.setSelectIndex(currentChannel.getHighFilterValue());
            mXoverSlopeLow.setSelectIndex(currentChannel.getLowFilterValue());

            mXpvHighPass.setValue(currentChannel.getHighFilterValue(), currentChannel.getHighFrequency(), currentChannel.getPassType() == 1);
            mXpvLowPass.setValue(currentChannel.getLowFilterValue(), currentChannel.getLowFrequency(), currentChannel.getPassType() == 2);

        }
//        mRbHighPass.setChecked(false);
//        mRbLowPass.setChecked(false);
        mXpvHighPass.postDelayed(mSetListenerTask, 300);
    }

    private Runnable mSetListenerTask = new Runnable() {
        @Override
        public void run() {
            if (mXpvHighPass == null) return;
            mXpvHighPass.setOnSlowCheckedChangeListener(XoverFragment.this);
            mXpvLowPass.setOnSlowCheckedChangeListener(XoverFragment.this);
            updateOk = true;
        }
    };

    @Override
    public void onRefreshFragment(int cmd) {
        if (cmd == CMD_SELECTED) {
            updateData();
        }
    }


   /* @OnLongClick({R.id.pbx_xover_high_reset, R.id.pbx_xover_low_reset})
    public boolean onReset(View view) {
        int id = view.getId();

        if (id == R.id.pbx_xover_high_reset) {
//            XOVER_SETTINGS.resetHighPass();
            XOVER_SETTINGS.getCurrentChannel().resetHighPass();
            XOVER_SETTINGS.getCurrentChannel().sendCmd();
        } else if (id == R.id.pbx_xover_low_reset) {
//            XOVER_SETTINGS.resetLowPass();
            XOVER_SETTINGS.getCurrentChannel().resetLowPass();
            XOVER_SETTINGS.getCurrentChannel().sendCmd();
        }
        updateData();
        return true;
    }*/

    private int findCheckedIndex(int groupChannel) {
        switch (groupChannel) {
            case 0:
            case 1:
                if (XOVER_SETTINGS.isGroup12()) {
                    return XOVER_SETTINGS.getBaseCh1() == 1 ? 0 : 1;
                } else return groupChannel;
            case 2:
            case 3:
                if (XOVER_SETTINGS.isGroup34()) {
                    return XOVER_SETTINGS.getBaseCh3() == 1 ? 2 : 3;
                } else return groupChannel;
            case 4:
            case 5:
                if (XOVER_SETTINGS.isGroup56()) {
                    return XOVER_SETTINGS.getBaseCh5() == 1 ? 4 : 5;
                } else return groupChannel;
            case 6:
            case 7:
                if (XOVER_SETTINGS.isGroup78()) {
                    return XOVER_SETTINGS.getBaseCh7() == 1 ? 6 : 7;
                } else return groupChannel;
        }
        return groupChannel;
    }



    private void onChannelChanged(int index) {
        YzDebug.d("onChannelChanged:"+ index);
        XOVER_SETTINGS.setLastChannel(index);
        updateOk = false;
        updateChValue();
        createChart();
    }

    /**
     * 重置CH1-CH6选中状态
     */
    private void resetSelectedChannelStatus() {
        int index = findCheckedIndex(XOVER_SETTINGS.getLastChannel());
        resetSelectedChannelStatus(index);
    }

    /**
     * 重置CH1-CH6选中状态
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void resetSelectedChannelStatus(int baseChannel) {
        YzDebug.d("resetSelectedChannelStatus:"+ baseChannel);
        for (PbxOutputSelectChannelView outputView : mOutputViews) {
            outputView.setSelected(false);
        }
        if (XOVER_SETTINGS.isGroup12() && (baseChannel == 0 || baseChannel == 1)) {
            mOutputViews.get(0).setSelected(true);
            mOutputViews.get(1).setSelected(true);
        } else if (XOVER_SETTINGS.isGroup34() && (baseChannel == 2 || baseChannel == 3)) {
            mOutputViews.get(2).setSelected(true);
            mOutputViews.get(3).setSelected(true);
        } else if (XOVER_SETTINGS.isGroup56() && (baseChannel == 4 || baseChannel == 5)) {
            mOutputViews.get(4).setSelected(true);
            mOutputViews.get(5).setSelected(true);
        } else if (XOVER_SETTINGS.isGroup78() && (baseChannel == 6 || baseChannel == 7)) {
            mOutputViews.get(6).setSelected(true);
            mOutputViews.get(7).setSelected(true);
        } else if (XOVER_SETTINGS.isGroupAll()) {
            for (PbxOutputSelectChannelView outputView : mOutputViews) {
                outputView.setSelected(true);
            }
        } else {
            mOutputViews.get(baseChannel).setSelected(true);
        }
        onChannelChanged(baseChannel);
    }


    private void setGroupState() {//ok
        mCbGroup[0].setSelected(XOVER_SETTINGS.isGroup12());
        mCbGroup[1].setSelected(XOVER_SETTINGS.isGroup34());
        mCbGroup[2].setSelected(XOVER_SETTINGS.isGroup56());
        mCbGroup[3].setSelected(XOVER_SETTINGS.isGroup78());
    }


//==========================================================================

    private View.OnClickListener onPassClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            YzDebug.d("Yang", " 1view:"+ view.isSelected());
            if(view.isSelected())
                return;
            boolean select = true;
            view.setSelected(select);
            for (ImageButton radioButton : mPassButtons) {
                if (view != radioButton) {
                    radioButton.setSelected(false);
                }
            }
            XoverChannel currentChannel = XOVER_SETTINGS.getCurrentChannel();
            switch (view.getId()){
                case R.id.pbx_xover_highpass:
                    setHighUiEnabled(true);
                    setLowUiEnabled(false);
                    mXpvHighPass.setEnabled(true);

                    mXpvLowPass.setEnabled(false);

                    setPassType(PASS_TYPE_HIGH_PASS, updateOk);
                    mXoverSlopeHigh.setSelectIndex(currentChannel.getHighFilterValue());
                    mXpvHighPass.setValue(currentChannel.getHighFilterValue(), currentChannel.getHighFrequency(), true);
                    break;
                case R.id.pbx_xover_lowpass:
                    setHighUiEnabled(false);
                    setLowUiEnabled(true);
                    mXpvHighPass.setEnabled(false);

                    mXpvLowPass.setEnabled(true);

                    setPassType(PASS_TYPE_LOW_PASS, updateOk);
                    mXoverSlopeLow.setSelectIndex(currentChannel.getLowFilterValue());
                    mXpvLowPass.setValue(currentChannel.getLowFilterValue(), currentChannel.getLowFrequency(), true);
                    break;
                case R.id.pbx_xover_high_bypass:
                    setHighUiEnabled(false);
                    setLowUiEnabled(false);
                    mXpvHighPass.setEnabled(false);

                    mXpvLowPass.setEnabled(false);

                    setPassType(PASS_TYPE_HIGH_BYPASS, updateOk);
                    break;
                case R.id.pbx_xover_low_bypass:
                    setHighUiEnabled(false);
                    setLowUiEnabled(false);
                    mXpvHighPass.setEnabled(false);

                    mXpvLowPass.setEnabled(false);

                    setPassType(PASS_TYPE_LOW_BYPASS, updateOk);
                    break;
            }
        }
    };

//    @OnCheckedChanged({R.id.cbx_high_switch, R.id.cbx_low_switch, R.id.cbx_low_by_switch, R.id.cbx_hight_by_switch})
//    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   /* @OnClick({R.id.pbx_xover_highpass,R.id.pbx_xover_lowpass,R.id.pbx_xover_low_bypass, R.id.pbx_xover_high_bypass})
    public void onPassClick(View view) {
        YzDebug.d("Yang", " 1view:"+ view.isSelected());
        if(view.isSelected())
            return;
        boolean select = true;
        view.setSelected(select);
        for (ImageButton radioButton : mPassButtons) {
            if (view != radioButton) {
                radioButton.setSelected(false);
            }
        }
        XoverChannel currentChannel = XOVER_SETTINGS.getCurrentChannel();
        switch (view.getId()){
            case R.id.pbx_xover_highpass:
                setHighUiEnabled(true);
                setLowUiEnabled(false);
                mXpvHighPass.setEnabled(true);

                mXpvLowPass.setEnabled(false);

                setPassType(PASS_TYPE_HIGH_PASS, updateOk);
                mXoverSlopeHigh.setSelectIndex(currentChannel.getHighFilterValue());
                mXpvHighPass.setValue(currentChannel.getHighFilterValue(), currentChannel.getHighFrequency(), true);
                break;
            case R.id.pbx_xover_lowpass:
                setHighUiEnabled(false);
                setLowUiEnabled(true);
                mXpvHighPass.setEnabled(false);

                mXpvLowPass.setEnabled(true);

                setPassType(PASS_TYPE_LOW_PASS, updateOk);
                mXoverSlopeLow.setSelectIndex(currentChannel.getLowFilterValue());
                mXpvLowPass.setValue(currentChannel.getLowFilterValue(), currentChannel.getLowFrequency(), true);
                break;
            case R.id.pbx_xover_high_bypass:
                setHighUiEnabled(false);
                setLowUiEnabled(false);
                mXpvHighPass.setEnabled(false);

                mXpvLowPass.setEnabled(false);

                setPassType(PASS_TYPE_HIGH_BYPASS, updateOk);
                break;
            case R.id.pbx_xover_low_bypass:
                setHighUiEnabled(false);
                setLowUiEnabled(false);
                mXpvHighPass.setEnabled(false);

                mXpvLowPass.setEnabled(false);

                setPassType(PASS_TYPE_LOW_BYPASS, updateOk);
                break;
        }
    }*/



    private void syncGroupChannel(int baseChannel, int[] channels) {
        XoverChannel baseXoverChannel = XOVER_SETTINGS.getXoverChannels().get(baseChannel);
        for (int channel : channels) {
            XOVER_SETTINGS.getXoverChannels().get(channel).syncGroupData(baseXoverChannel);
        }
    }

    // -----------------
    private void setPassType(int passType, boolean sendCmd) {
        XoverChannel xoverChannel = XOVER_SETTINGS.getXoverChannels().get(XOVER_SETTINGS.getLastChannel());
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        int channel = XOVER_SETTINGS.getLastChannel();
        if (channel == 0 || channel == 1) {
            if (XOVER_SETTINGS.isGroup12()) {
                xoverChannels.get(0).setPassType(passType);
                xoverChannels.get(1).setPassType(passType);
            } else {
                xoverChannel.setPassType(passType);
            }
        } else if (channel == 2 || channel == 3) {
            if (XOVER_SETTINGS.isGroup34()) {
                xoverChannels.get(2).setPassType(passType);
                xoverChannels.get(3).setPassType(passType);
            } else {
                xoverChannel.setPassType(passType);
            }
        } else if (channel == 4 || channel == 5) {
            if (XOVER_SETTINGS.isGroup56()) {
                xoverChannels.get(4).setPassType(passType);
                xoverChannels.get(5).setPassType(passType);
            } else {
                xoverChannel.setPassType(passType);
            }
        } else if (channel == 6 || channel == 7) {
            if (XOVER_SETTINGS.isGroup78()) {
                xoverChannels.get(6).setPassType(passType);
                xoverChannels.get(7).setPassType(passType);
            } else {
                xoverChannel.setPassType(passType);
            }
        } else {
            xoverChannel.setPassType(passType);
        }
        if (sendCmd) {
            xoverChannel.sendCmd();
        }
        refreshChat();
    }

    private void setLowFilterValue(int value) {
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        int channel = XOVER_SETTINGS.getLastChannel();
        switch (channel){
            case 0:
            case 1:
                if (XOVER_SETTINGS.isGroup12()) {
                    xoverChannels.get(0).setLowFilterValue(value);
                    xoverChannels.get(1).setLowFilterValue(value);
                } else {
                    xoverChannel.setLowFilterValue(value);
                }
                break;
            case 2:
            case 3:
                if (XOVER_SETTINGS.isGroup34()) {
                    xoverChannels.get(2).setLowFilterValue(value);
                    xoverChannels.get(3).setLowFilterValue(value);
                } else {
                    xoverChannel.setLowFilterValue(value);
                }
                break;
            case 4:
            case 5:
                if (XOVER_SETTINGS.isGroup56()) {
                    xoverChannels.get(4).setLowFilterValue(value);
                    xoverChannels.get(5).setLowFilterValue(value);
                } else {
                    xoverChannel.setLowFilterValue(value);
                }
                break;
            case 6:
            case 7:
                if (XOVER_SETTINGS.isGroup78()) {
                    xoverChannels.get(6).setLowFilterValue(value);
                    xoverChannels.get(7).setLowFilterValue(value);
                } else {
                    xoverChannel.setLowFilterValue(value);
                }
                break;
            default:
                if (xoverChannel != null){
                    xoverChannel.setLowFilterValue(value);
                }
                break;
        }
        refreshChat();
    }

    private void setLowFilterType(int value) {
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        int channel = XOVER_SETTINGS.getLastChannel();
        if (channel == 0 || channel == 1) {
            if (XOVER_SETTINGS.isGroup12()) {
                xoverChannels.get(0).setLowFilterType(value);
                xoverChannels.get(1).setLowFilterType(value);
            } else {
                xoverChannel.setLowFilterType(value);
            }
        } else if (channel == 2 || channel == 3) {
            if (XOVER_SETTINGS.isGroup34()) {
                xoverChannels.get(2).setLowFilterType(value);
                xoverChannels.get(3).setLowFilterType(value);
            } else {
                xoverChannel.setLowFilterType(value);
            }
        } else if (channel == 4 || channel == 5) {
            if (XOVER_SETTINGS.isGroup56()) {
                xoverChannels.get(4).setLowFilterType(value);
                xoverChannels.get(5).setLowFilterType(value);
            } else {
                xoverChannel.setLowFilterType(value);
            }
        } else if (channel == 6 || channel == 7) {
            if (XOVER_SETTINGS.isGroup78()) {
                xoverChannels.get(6).setLowFilterType(value);
                xoverChannels.get(7).setLowFilterType(value);
            } else {
                xoverChannel.setLowFilterType(value);
            }
        } else {
            xoverChannel.setLowFilterType(value);
        }
        refreshChat();
    }


    private void setHighFilterType(int value) {
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        int channel = XOVER_SETTINGS.getLastChannel();
        if (channel == 0 || channel == 1) {
            if (XOVER_SETTINGS.isGroup12()) {
                xoverChannels.get(0).setHighFilterType(value);
                xoverChannels.get(1).setHighFilterType(value);
            } else {
                xoverChannel.setHighFilterType(value);
            }
        } else if (channel == 2 || channel == 3) {
            if (XOVER_SETTINGS.isGroup34()) {
                xoverChannels.get(2).setHighFilterType(value);
                xoverChannels.get(3).setHighFilterType(value);
            } else {
                xoverChannel.setHighFilterType(value);
            }
        } else if (channel == 4 || channel == 5) {
            if (XOVER_SETTINGS.isGroup56()) {
                xoverChannels.get(4).setHighFilterType(value);
                xoverChannels.get(5).setHighFilterType(value);
            } else {
                xoverChannel.setHighFilterType(value);
            }
        } else if (channel == 6 || channel == 7) {
            if (XOVER_SETTINGS.isGroup78()) {
                xoverChannels.get(6).setHighFilterType(value);
                xoverChannels.get(7).setHighFilterType(value);
            } else {
                xoverChannel.setHighFilterType(value);
            }
        } else {
            xoverChannel.setHighFilterType(value);
        }
        refreshChat();
    }

    private void setHighFilterValue(int value) {
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        int channel = XOVER_SETTINGS.getLastChannel();
        if (channel == 0 || channel == 1) {
            if (XOVER_SETTINGS.isGroup12()) {
                xoverChannels.get(0).setHighFilterValue(value);
                xoverChannels.get(1).setHighFilterValue(value);
            } else {
                xoverChannel.setHighFilterValue(value);
            }
        } else if (channel == 2 || channel == 3) {
            if (XOVER_SETTINGS.isGroup34()) {
                xoverChannels.get(2).setHighFilterValue(value);
                xoverChannels.get(3).setHighFilterValue(value);
            } else {
                xoverChannel.setHighFilterValue(value);
            }
        } else if (channel == 4 || channel == 5) {
            if (XOVER_SETTINGS.isGroup56()) {
                xoverChannels.get(4).setHighFilterValue(value);
                xoverChannels.get(5).setHighFilterValue(value);
            } else {
                xoverChannel.setHighFilterValue(value);
            }
        } else if (channel == 6 || channel == 7) {
            if (XOVER_SETTINGS.isGroup78()) {
                xoverChannels.get(6).setHighFilterValue(value);
                xoverChannels.get(7).setHighFilterValue(value);
            } else {
                xoverChannel.setHighFilterValue(value);
            }
        } else {
            xoverChannel.setHighFilterValue(value);
        }
        refreshChat();
    }



    private void setHighPassValue(int filterType, int value, int freq) {//ok
        List<XoverChannel> xoverChannels = XOVER_SETTINGS.getXoverChannels();
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        int channel = XOVER_SETTINGS.getLastChannel();
        if (channel == 0 || channel == 1) {
            if (XOVER_SETTINGS.isGroup12()) {
                xoverChannels.get(0).setHighPassValue(filterType, value, freq);
                xoverChannels.get(1).setHighPassValue(filterType, value, freq);
            } else {
                xoverChannel.setHighPassValue(filterType, value, freq);
            }
        } else if (channel == 2 || channel == 3) {
            if (XOVER_SETTINGS.isGroup34()) {
                xoverChannels.get(2).setHighPassValue(filterType, value, freq);
                xoverChannels.get(3).setHighPassValue(filterType, value, freq);
            } else {
                xoverChannel.setHighPassValue(filterType, value, freq);
            }
        } else if (channel == 4 || channel == 5) {
            if (XOVER_SETTINGS.isGroup56()) {
                xoverChannels.get(4).setHighPassValue(filterType, value, freq);
                xoverChannels.get(5).setHighPassValue(filterType, value, freq);
            } else {
                xoverChannel.setHighPassValue(filterType, value, freq);
            }
        } else if (channel == 6 || channel == 7) {
            if (XOVER_SETTINGS.isGroup78()) {
                xoverChannels.get(6).setHighPassValue(filterType, value, freq);
                xoverChannels.get(7).setHighPassValue(filterType, value, freq);
            } else {
                xoverChannel.setHighPassValue(filterType, value, freq);
            }
        } else {
            xoverChannel.setHighPassValue(filterType, value, freq);
        }
        refreshChat();
    }

    private void createChart() {
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        EqChannel eqChannel = DspSettings.EQ_SETTINGS.getEqChannels().get(xoverChannel.getChannel());
        int qSize = eqChannel.getQ().length;
        int gainSize = eqChannel.getGain().length;
        float[] chartQ = new float[qSize];
        int[] chartGain = new int[gainSize];
        for (int i = 0; i < qSize; i++) {
            chartQ[i] = eqChannel.getQ()[i] / 100f;
//            chartGain[i] = (eqChannel.getGain()[i] - 200) / 10;
            //yangzhong
            chartGain[i] = (eqChannel.getGain()[i] + SettingConstants.GAIN_MIN) / 10;
        }

        byte[] octTypes = new byte[]{(byte) DrawFunction.getOctType(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue()), (byte) DrawFunction.getOctType(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue())};
        mEqChart.setColor(eqChannel.getChannel());
        mEqChart.setPEQ(chartQ, chartGain, eqChannel.getFreq());
        mEqChart.setXover(new int[]{xoverChannel.getLowFrequency(), xoverChannel.getHighFrequency()}, octTypes);
        mEqChart.reArray();

    }

    private void refreshChat() {
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        byte[] octTypes = new byte[]{(byte) DrawFunction.getOctType(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue()), (byte) DrawFunction.getOctType(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue())};
        int passtype = xoverChannel.getPassType();
        int lowFreq = xoverChannel.getLowFrequency();
        int highFreq = xoverChannel.getHighFrequency();
        switch (passtype){
            case PASS_TYPE_HIGH_BYPASS:
//                highFreq = 20;
//                break;
            case PASS_TYPE_LOW_BYPASS:
                lowFreq = 20000;
                highFreq = 20;
                break;
        }
        mEqChart.resetXover(new int[]{lowFreq, highFreq}, octTypes);
    }

    //---------------

    @Override
    public void onDestroy() {
        mKeyboardChangeListener.destroy();
        super.onDestroy();
    }

    private View.OnLongClickListener onGroupLongClick = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            int id = view.getId();
            switch (view.getId()) {
                case R.id.xover_link1:
                    if (XOVER_SETTINGS.isGroup12()) {
                        mCbGroup[0].setSelected(false);
                        if (XOVER_SETTINGS.getBaseCh1() == 1) {
                            mOutputViews.get(1).setSelected(false);
                        } else if (XOVER_SETTINGS.getBaseCh2() == 1) {
                            mOutputViews.get(0).setSelected(false);
                        }
                        XOVER_SETTINGS.setBaseCh1(0);
                        XOVER_SETTINGS.setBaseCh2(0);
                    }
                    break;
                case R.id.xover_link2:
                    if (XOVER_SETTINGS.isGroup34()) {
                        mCbGroup[1].setSelected(false);
                        if (XOVER_SETTINGS.getBaseCh3() == 1) {
                            mOutputViews.get(3).setSelected(false);
                        } else if (XOVER_SETTINGS.getBaseCh4() == 1) {
                            mOutputViews.get(2).setSelected(false);
                        }
                        XOVER_SETTINGS.setBaseCh3(0);
                        XOVER_SETTINGS.setBaseCh4(0);
                    }
                    break;
                case R.id.xover_link3:
                    if (XOVER_SETTINGS.isGroup56()) {
                        mCbGroup[2].setSelected(false);
                        if (XOVER_SETTINGS.getBaseCh5() == 1) {
                            mOutputViews.get(5).setSelected(false);
                        } else if (XOVER_SETTINGS.getBaseCh6() == 1) {
                            mOutputViews.get(4).setSelected(false);
                        }
                        XOVER_SETTINGS.setBaseCh5(0);
                        XOVER_SETTINGS.setBaseCh6(0);
                    }
                    break;
                case R.id.xover_link4:
                    mCbGroup[3].setSelected(false);
                    if (isSupportGroupAll) {
                        if (XOVER_SETTINGS.isGroupAll()) {
                            final int[] groupInfo = XOVER_SETTINGS.getGroupInfo();
                            int index = 0;
                            for (int value : groupInfo) {
                                mOutputViews.get(index).setSelected(value != 0);
                                index++;
                            }
                            Arrays.fill(groupInfo, 0);
                            XOVER_SETTINGS.setGroupInfo(groupInfo);
                        }
                    } else {
                        if (XOVER_SETTINGS.isGroup78()) {
                            if (XOVER_SETTINGS.getBaseCh7() == 1) {
                                mOutputViews.get(7).setSelected(false);
                            } else if (XOVER_SETTINGS.getBaseCh8() == 1) {
                                mOutputViews.get(6).setSelected(false);
                            }
                            XOVER_SETTINGS.setBaseCh7(0);
                            XOVER_SETTINGS.setBaseCh8(0);
                        }
                    }
                    return true;

            };
            return true;
        }
    };



   /* @OnLongClick({R.id.xover_link1, R.id.xover_link2, R.id.xover_link3, R.id.xover_link4})
    public boolean onGroupLongClick(View view) {
        YzDebug.d("onGroupLongClick:"+ view.isSelected());
        if (!view.isSelected())return true;
        int id = view.getId();
        switch (view.getId()) {
            case R.id.xover_link1:
                if (XOVER_SETTINGS.isGroup12()) {
                    mCbGroup[0].setSelected(false);
                    if (XOVER_SETTINGS.getBaseCh1() == 1) {
                        mOutputViews.get(1).setSelected(false);
                    } else if (XOVER_SETTINGS.getBaseCh2() == 1) {
                        mOutputViews.get(0).setSelected(false);
                    }
                    XOVER_SETTINGS.setBaseCh1(0);
                    XOVER_SETTINGS.setBaseCh2(0);
                }
                break;
            case R.id.xover_link2:
                if (XOVER_SETTINGS.isGroup34()) {
                    mCbGroup[1].setSelected(false);
                    if (XOVER_SETTINGS.getBaseCh3() == 1) {
                        mOutputViews.get(3).setSelected(false);
                    } else if (XOVER_SETTINGS.getBaseCh4() == 1) {
                        mOutputViews.get(2).setSelected(false);
                    }
                    XOVER_SETTINGS.setBaseCh3(0);
                    XOVER_SETTINGS.setBaseCh4(0);
                }
                break;
            case R.id.xover_link3:
                if (XOVER_SETTINGS.isGroup56()) {
                    mCbGroup[2].setSelected(false);
                    if (XOVER_SETTINGS.getBaseCh5() == 1) {
                        mOutputViews.get(5).setSelected(false);
                    } else if (XOVER_SETTINGS.getBaseCh6() == 1) {
                        mOutputViews.get(4).setSelected(false);
                    }
                    XOVER_SETTINGS.setBaseCh5(0);
                    XOVER_SETTINGS.setBaseCh6(0);
                }
                break;
            case R.id.xover_link4:
                mCbGroup[3].setSelected(false);
                if (isSupportGroupAll) {
                    if (XOVER_SETTINGS.isGroupAll()) {
                        final int[] groupInfo = XOVER_SETTINGS.getGroupInfo();
                        int index = 0;
                        for (int value : groupInfo) {
                            mOutputViews.get(index).setSelected(value != 0);
                            index++;
                        }
                        Arrays.fill(groupInfo, 0);
                        XOVER_SETTINGS.setGroupInfo(groupInfo);
                    }
                } else {
                    if (XOVER_SETTINGS.isGroup78()) {
                        if (XOVER_SETTINGS.getBaseCh7() == 1) {
                            mOutputViews.get(7).setSelected(false);
                        } else if (XOVER_SETTINGS.getBaseCh8() == 1) {
                            mOutputViews.get(6).setSelected(false);
                        }
                        XOVER_SETTINGS.setBaseCh7(0);
                        XOVER_SETTINGS.setBaseCh8(0);
                    }
                }
                break;
        }
        return true;
    }*/

    private View.OnClickListener onGroupClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.isSelected())return;
            boolean selected = !view.isSelected();
//        view.setSelected(selected);
            Grouping index = Grouping.UNKNOWN;
            int id = view.getId();
            switch (view.getId()) {
                case R.id.xover_link1:
                    index = Grouping.GROUP1_2;
                    if (XOVER_SETTINGS.isGroup12()) return;
                    break;
                case R.id.xover_link2:
                    index = Grouping.GROUP3_4;
                    if (XOVER_SETTINGS.isGroup34()) return;
                    break;
                case R.id.xover_link3:
                    index = Grouping.GROUP5_6;
                    if (XOVER_SETTINGS.isGroup56()) return;
                    break;
                case R.id.xover_link4:
                    if (isSupportGroupAll) {
                        index = Grouping.GROUP_ALL;
                        if (XOVER_SETTINGS.isGroupAll()) return;
                    } else {
                        index = Grouping.GROUP7_8;
                        if (XOVER_SETTINGS.isGroup78()) return;
                    }
                    break;
            }
            mGroupingDialog.setSelectGroupIndex(index);
            mGroupingDialog.show();
        }
    };

//    @OnClick({R.id.xover_link1, R.id.xover_link2, R.id.xover_link3, R.id.xover_link4})
//    public void onGroupClick(View view) {
//        if (view.isSelected())return;
//        boolean selected = !view.isSelected();
////        view.setSelected(selected);
//        Grouping index = Grouping.UNKNOWN;
//        int id = view.getId();
//        switch (view.getId()) {
//            case R.id.xover_link1:
//                index = Grouping.GROUP1_2;
//                if (XOVER_SETTINGS.isGroup12()) return;
//                break;
//            case R.id.xover_link2:
//                index = Grouping.GROUP3_4;
//                if (XOVER_SETTINGS.isGroup34()) return;
//                break;
//            case R.id.xover_link3:
//                index = Grouping.GROUP5_6;
//                if (XOVER_SETTINGS.isGroup56()) return;
//                break;
//            case R.id.xover_link4:
//                if (isSupportGroupAll) {
//                    index = Grouping.GROUP_ALL;
//                    if (XOVER_SETTINGS.isGroupAll()) return;
//                } else {
//                    index = Grouping.GROUP7_8;
//                    if (XOVER_SETTINGS.isGroup78()) return;
//                }
//                break;
//        }
//        mGroupingDialog.setSelectGroupIndex(index);
//        mGroupingDialog.show();
//    }
    @Override
    public void onGrouping(Grouping groupIndex, int groupChannel) {
        boolean isChanged = false;
        int[] updateValues = new int[8];
        switch (groupIndex) {
            case GROUP1_2:
            case GROUP3_4:
            case GROUP5_6:
            case GROUP7_8:
                isChanged = true;
                if (XOVER_SETTINGS.isGroupAll()) {
                    mCbGroup[3].setSelected(false);
                    Arrays.fill(updateValues, 0);
                } else {
                    Arrays.fill(updateValues, -1);
                }
                updateValues[groupChannel] = 1;
                break;
            case GROUP1_2_3_4:
            case GROUP5_6_7_8:
                isChanged = true;
                updateValues[groupChannel] = 11;
                break;
            case GROUP_ALL:
                isChanged = true;
                Arrays.fill(updateValues, 0);
                updateValues[groupChannel] = 1111;
                break;
        }
        if (isChanged) {
            XOVER_SETTINGS.setGroupInfo(updateValues);
            syncGroupChannel(groupChannel, groupIndex.getGroupIndexs());
        }

        if (groupChannel == -1) {
            resetSelectedChannelStatus();
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                resetSelectedChannelStatus(groupChannel);
            }
            XOVER_SETTINGS.getXoverChannels().get(groupChannel).sendCmd();
        }
        setGroupState();

    }

    private void setLowPassValue(int filterType, int value, int freq) {
        processWithGroup(xoverChannel -> xoverChannel.setLowPassValue(filterType, value, freq));
        updateChat();
    }

    private void setHighFrequency(int frequency) {
        processWithGroup(xoverChannel -> xoverChannel.setHighFrequency(frequency));
        updateChat();
    }
    private void setLowFrequency(int frequency) {
        processWithGroup(xoverChannel -> xoverChannel.setLowFrequency(frequency));
        updateChat();
    }
    private interface IProcessWithGroup {
        void onXoverChannelChange(XoverChannel channel);
    }
    private void processWithGroup(IProcessWithGroup callback) {
        if (callback == null) return;
        int channel = XOVER_SETTINGS.getLastChannel();
        int[] indexs = null;
        if (XOVER_SETTINGS.isGroupAll()) {
            indexs = Grouping.GROUP_ALL.getGroupIndexs();
        } else if (XOVER_SETTINGS.isGroup12()) {
            if (channel == 0 || channel == 1) {
                indexs = Grouping.GROUP1_2.getGroupIndexs();
            }
        } else if (XOVER_SETTINGS.isGroup34()) {
            if (channel == 2 || channel == 3) {
                indexs = Grouping.GROUP3_4.getGroupIndexs();
            }
        } else if (XOVER_SETTINGS.isGroup56()) {
            if (channel == 4 || channel == 5) {
                indexs = Grouping.GROUP5_6.getGroupIndexs();
            }
        } else if (XOVER_SETTINGS.isGroup78()) {
            if (channel == 6 || channel == 7) {
                indexs = Grouping.GROUP7_8.getGroupIndexs();
            }
        }
        if (indexs == null) {
            indexs = new int[]{channel};
        }
        final List<XoverChannel> cls = XOVER_SETTINGS.getXoverChannels();
        for (int i : indexs) {
            if (i >= cls.size()) break;
            callback.onXoverChannelChange(cls.get(i));
        }
    }

    private void updateChat() {
        XoverChannel xoverChannel = XOVER_SETTINGS.getCurrentChannel();
        switch (xoverChannel.getPassType()) {
            case PASS_TYPE_HIGH_PASS:
            case PASS_TYPE_LOW_PASS:
            case PASS_TYPE_BAND_PASS:
                byte[] octTypes = new byte[]{(byte) DrawFunction.getOctType(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue()), (byte) DrawFunction.getOctType(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue())};
                mEqChart.resetXover(new int[]{xoverChannel.getLowFrequency(), xoverChannel.getHighFrequency()}, octTypes);
                break;
            case PASS_TYPE_HIGH_BYPASS:
            case PASS_TYPE_LOW_BYPASS:
            case PASS_TYPE_BAND_BPASS:
                octTypes = new byte[]{(byte) DrawFunction.getOctType(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue()), (byte) DrawFunction.getOctType(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue())};
                Arrays.fill(octTypes, (byte) 1);
                mEqChart.resetXover(new int[]{SettingConstants.FREQ_MAX, SettingConstants.FREQ_MIN}, octTypes);
                break;
        }

    }

    private final PbxOutputSelectChannelView.OnCheckChangeListener mChannelSelectListener = new PbxOutputSelectChannelView.OnCheckChangeListener() {

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onCheckChange(PbxOutputSelectChannelView view, boolean isChecked) {
            for (PbxOutputSelectChannelView outputView : mOutputViews) {
                if (outputView.getId() != view.getId()) {
                    outputView.setSelected(false);
                }
            }
            final int index = view.getChannelIndex();
            XOVER_SETTINGS.setLastChannel(index);
            resetSelectedChannelStatus(findCheckedIndex(index));
            mPbxOutputSelectChannelsLayout.postInvalidate();
        }
    };
    private final PbxCircleSeekBar.OnCircleSeekBarFunListener mOnCircleSeekBarFunListener = new PbxCircleSeekBar.OnCircleSeekBarFunListener() {
        @Override
        public void onFunClick(PbxCircleSeekBar bar, PbxCircleSeekBar.FunId id) {
            if (!bar.isEnabled())return;
            int value = 0;
            switch (id){
                case FUN_ID_SUB:
                    int freq = FreqUtil.parseProgressToFreq(bar.getProgress());
                    if (freq > 20){
                        value = freq - 1;
                    }
                    break;
                case FUN_ID_ADD:
                    freq = FreqUtil.parseProgressToFreq(bar.getProgress());
                    if (freq < 20000){
                        value = freq + 1;
                    }
                    break;
            }
            YzDebug.d("onFunClick value:"+ value );
            if (value != 0){
                bar.setProgress(FreqUtil.parseFreqToProgress(value));
                mCircleSeekBarListener.onProgressChange(bar, bar.getProgress(), MotionEvent.ACTION_UP);
            }
        }
    };
    private final PbxCircleSeekBar.OnCircleSeekBarListener mCircleSeekBarListener = new PbxCircleSeekBar.OnCircleSeekBarListener() {
        private long mLastSendCmdTime;
        @Override
        public void onProgressChange(PbxCircleSeekBar seekBar, int progress, int action) {
            int freq = FreqUtil.parseProgressToFreq(progress);
            YzDebug.d("Yang","----------:"+ progress +" " + freq);
            switch (seekBar.getId()){
                case R.id.pbx_xover_seekbar_high:
                    setHighFrequency(freq);
                    mEditTextHigh.setText(String.valueOf(freq));
                    break;
                case R.id.pbx_xover_seekbar_low:
                    setLowFrequency(freq);
                    mEditTextLow.setText(String.valueOf(freq));
                    break;
            }
        }
    };
    //====================
    private XoverEditText.ValidEditTextListener2 mEditTextListener = new XoverEditText.ValidEditTextListener2() {
        @Override
        public void onValidEditTextValue(XoverEditText et) {
            final Editable editable = et.getText();
            int frenquency = 20;
            final String text = editable == null?null:editable.toString();
            if (TextUtils.isEmpty(text)){
                frenquency = 20;
//                    etFrequency.setText("20");
            }else {
                try {
                    frenquency = Integer.parseInt(editable.toString());
                }catch (Exception e){}
                if (frenquency < SettingConstants.FREQ_MIN) {
                    frenquency = SettingConstants.FREQ_MIN;

                } else if (frenquency > SettingConstants.FREQ_MAX) {
                    frenquency = SettingConstants.FREQ_MAX;
                }
            }
            et.setText(String.valueOf(frenquency));
            switch (et.getId()){
                case R.id.pbx_xover_hight_et:
                    mCircleSeekBarHigh.setProgress(FreqUtil.parseFreqToProgress(frenquency));
                    setHighFrequency(frenquency);
                    break;
                case R.id.pbx_xover_low_et:
                    mCircleSeekBarLow.setProgress(FreqUtil.parseFreqToProgress(frenquency));
                    setLowFrequency(frenquency);
                    break;
            }

        }
    };

    private final XoverSlopeWheelView.SelectItemListener mSelectItemListener = (view, index) -> {
        switch (view.getId()){
            case R.id.pbx_xover_whv_high:
                XoverChannel channelData = XOVER_SETTINGS.getCurrentChannel();
                setHighFilterValue(index);
                if (!updateOk) return;
                channelData.sendCmd();
                break;
            case R.id.pbx_xover_whv_low:
                 channelData = XOVER_SETTINGS.getCurrentChannel();
                setLowFilterValue(index);
                if (!updateOk) return;
                channelData.sendCmd();
                break;
        }
    };
    @Override
    public void onCheckedChanged(XOverPassView view, int frenquency, boolean sendCmd) {


        XoverChannel channelData = XOVER_SETTINGS.getCurrentChannel();
        int id = view.getId();
        if (id == R.id.xpv_high_pass) {
            setHighFrequency(frenquency);
//            setPassType(1, false);
//            setHighPassValue(channelData.getHighFilterType(), view.getCheckId(), view.getFrenquency());
        } else if (id == R.id.xpv_low_pass) {
            setLowFrequency(frenquency);
//            setPassType(2, false);
//            setLowPassValue(channelData.getLowFilterType(), view.getCheckId(), view.getFrenquency());
        }
        if (sendCmd) {
            channelData.sendCmd();
        }
    }



    private void setHighUiEnabled(boolean enabled){
        mXoverSlopeHigh.setEnabled(enabled);
        mCircleSeekBarHigh.setEnabled(enabled);
        mEditTextHigh.setEnabled(enabled);
    }
    private void setLowUiEnabled(boolean enabled){
        mXoverSlopeLow.setEnabled(enabled);
        mCircleSeekBarLow.setEnabled(enabled);
        mEditTextLow.setEnabled(enabled);
    }
}
